/*
 * 文件名：CustAnalysisServiceImp.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2013-2-21
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.customer.custanalysis.service.imp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;

import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.Result;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.customer.custanalysis.dao.CustAnalysisDao;
import com.wootide.cms.service.customer.custanalysis.pojo.CustomerDetail;
import com.wootide.cms.service.customer.custanalysis.service.CustAnalysisService;
import com.wootide.cms.service.customer.custanalysis.transaction.CustAnalysisTransaction;
import com.wootide.cms.service.customer.custmanager.pojo.Customer;
import com.wootide.cms.service.facility.room.roominfo.pojo.RoomInfo;
import com.wootide.cms.service.facility.room.roominfo.pojo.RoomPeriod;
import com.wootide.cms.service.reservation.bookinfo.pojo.BookOrderInfo;

public class CustAnalysisServiceImp implements CustAnalysisService
{
    
    // 注入运行日志
    private RunLog runLog;
    
    //用户事务层实例
    private CustAnalysisTransaction custAnalysisTransaction;
    
    //用户数据访问层实例
    private CustAnalysisDao custAnalysisDao;
    
    @Override
    public Result queryForPages(Page page, QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryConditon=" + queryConditon);
        
        // 定义返回结果
        Result result = new Result();
        try
        {
            QueryResults queryResults = custAnalysisTransaction.queryForPages(page,
                    queryConditon);
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query BookOrderInfo Success!");
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Query BookOrderInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            //异常
            runLog.error("Query BookOrderInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    public RunLog getRunLog()
    {
        return runLog;
    }
    
    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }
    
    public CustAnalysisTransaction getCustAnalysisTransaction()
    {
        return custAnalysisTransaction;
    }
    
    public void setCustAnalysisTransaction(
            CustAnalysisTransaction custAnalysisTransaction)
    {
        this.custAnalysisTransaction = custAnalysisTransaction;
    }
    
    public CustAnalysisDao getCustAnalysisDao()
    {
        return custAnalysisDao;
    }
    
    public void setCustAnalysisDao(CustAnalysisDao custAnalysisDao)
    {
        this.custAnalysisDao = custAnalysisDao;
    }
    
    @Override
    public Result getCustomerDetail(CustomerDetail customerDetail,
            Integer innerIndex)
    {
        Result result = new Result();
        SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat st = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //查找出消费总次数
        int num = custAnalysisDao.findNumBookOrderInfo(innerIndex);
        
        //查询会员基本信息
        Customer customer = custAnalysisDao.findCustomerInfoByCustomerId(innerIndex);
        customerDetail.setCardNo(customer.getCardNo());
        customerDetail.setCustName(customer.getCustName());
        customerDetail.setCustLevel(customer.getClientLevelId()
                .getClientLevelName());
        customerDetail.setTypeID(customer.getTypeID());
        customerDetail.setNumberID(customer.getNumberID());
        customerDetail.setProfession(customer.getProfession());
        customerDetail.setTel(customer.getTel());
        
        //查出会员最近3次消费时间
        List<BookOrderInfo> list = custAnalysisDao.findBookOrderInfoLastTimeByCustomerId(innerIndex);
        List<String> times = new ArrayList<String>();
        List<String> rooms = new ArrayList<String>();
        if (!list.isEmpty())
        {
            for (BookOrderInfo book : list)
            {
                times.add(sft.format(book.getDeneratedDate()));
                if (times.size() >= 3)
                {
                    break;
                }
            }
            customerDetail.setTime(times);
        }
        
        //查出会员使用习惯
        RoomPeriod room = custAnalysisDao.findRoomPeriodByOrderNum(list.get(0)
                .getOrderNum());
        customerDetail.setStartTime(st.format(room.getStartTime()));
        customerDetail.setEndTime(st.format(room.getEndTime()));
        customerDetail.setRoomId(room.getRoomInfo().getRoomNum() + "->"
                + room.getRoomInfo().getRoomName());
        customerDetail.setLongestRetention((room.getEndTime().getTime() - room.getStartTime()
                .getTime()) / 3600000);
        customerDetail.setNum(num);
        //查出客户使用的所有房间信息
        List<RoomInfo> listRoom = custAnalysisDao.findAllRoomByCustomerId(innerIndex);
        if (!listRoom.isEmpty())
        {
            for (RoomInfo roomInfo : listRoom)
            {
                rooms.add(roomInfo.getRoomNum() + "->" + roomInfo.getRoomName());
            }
            customerDetail.setHistoryRoom(rooms);
        }
        result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        return result;
    }
}
